﻿using Microsoft.Practices.Prism.Regions;
using Microsoft.Practices.Unity;
using Product_2_3.ServiceReference1;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data.Services.Client;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Product_2_3
{
    /// <summary>
    /// Interaction logic for UserControl1.xaml
    /// </summary>
    public partial class Product_2_3_View : UserControl, IActivityView, IInputView
    {
        ServiceReference1.Container context = new ServiceReference1.Container(new Uri("http://localhost:1179/odata"));

        private InputPresenter _presenter;
        private ActivityPresenter presenter;
        private readonly IUnityContainer container;
        private readonly IRegionManager rManager;

        public Product_2_3_View()
        {
            InitializeComponent();
        }

        public Product_2_3_View(InputPresenter presenter2, ActivityPresenter presenter, IUnityContainer c, IRegionManager r) : this()
        {
            _presenter = presenter2;
            _presenter.View = this;

            this.presenter = presenter;
            presenter.View = this;

            container = c;
            rManager = r;
        }

        public string Number
        {
            get
            {
                return this.pM_LotNumberTextBox1.Text.ToString();
            }
        }

        public string Number2
        {
            get
            {
                return this.pM_IdTextBox1.Text.ToString();
            }
        }


        //C#에서 CRUD를 하려면 이렇게 해야한다.
        //1. 먼저 using에 using System.Data.Services.Client; 를 써서 클라이언트의 서비스를 불러온다.
        //2.  DataServiceCollection<table명> lstcontext = new DataServiceCollection<예로 여기선 Product_Mid tabel을 사용>();
        //위와같이 사용할 데이터베이스의 Table명을  DataServiceCollection<>를 사용해서 명명한다.
        // 이렇게하고   context.DeleteObject();  이렇게하고   context.UpdateObject();등을 사용해서 데이터베이트에 업데이트 딜리트시킬수 있다.
        //그리고 자세한 CRUD는 아래의 각 CRUD버튼의 코딩을 보고 이해할것!

        private void RadioButton_Click(object sender, RoutedEventArgs e)
        {
            Uri viewNav = new Uri("Product_1_3_View", UriKind.Relative);
            rManager.RequestNavigate(RegionNames.MainContentRegion, viewNav);
        }

        private void UpdateButton_Click(object sender, RoutedEventArgs e)
        {
            Product_Mid product_Mid = context.Product_Mid.Where(kkk => kkk.PM_Id == Int32.Parse(this.pM_IdTextBox1.Text)).SingleOrDefault();

            product_Mid.PM_LotNumber = this.pM_LotNumberTextBox1.Text;



            product_Mid.PM_State = this.pM_StateTextBox1.Text;

            context.UpdateObject(product_Mid);
            context.SaveChanges();
        }

        private void DeleteButton_Click_1(object sender, RoutedEventArgs e)
        {

            var idx = this.product_MidDataGrid.SelectedIndex;
            Object product = this.product_MidDataGrid.Items.GetItemAt(idx);


            context.DeleteObject(product);
            context.SaveChanges();

            DataServiceCollection<Product_Mid> lstContext = new DataServiceCollection<Product_Mid>();

            lstContext.LoadCompleted += (objsender, eventArg) =>
            {
                if (eventArg.Error == null)
                {
                    this.product_MidDataGrid.DataContext = lstContext;
                }
            };

            lstContext.LoadAsync(context.Product_Mid);
        }

        public void SetLotNumber(string number)
        {
            var sss =
               from kkk in context.Product_Mid
               where kkk.PM_LotNumber == number
               select kkk;

            this.product_MidDataGrid.DataContext =
                sss;

            this.pM_LotNumberTextBox1.Text = number;
        }
        //public void SetDate(string year, string month, string day)
        //{
        //    int a = Convert.ToInt32(year);
        //    int b = Convert.ToInt32(month);
        //    int c = Convert.ToInt32(day);
        //    this.pM_DateDatePicker1.SelectedDate = new DateTime(a, b, c);
        //}

        private void product_MidDataGrid_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e)
        {
            this.grid2.DataContext = this.product_MidDataGrid.SelectedItem;
        }

        private void RadioButton_Click_1(object sender, RoutedEventArgs e)
        {
            Uri viewNav = new Uri("LoNumberView", UriKind.Relative);
            rManager.RequestNavigate(RegionNames.MainContentRegion, viewNav);

            Result(this, null);
        }
        public event EventHandler Result = delegate { };
    }
}
